package org.njbdqn.ali.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DbManager {
	private Connection conn;
	private PreparedStatement pstate;
	private ResultSet rs;
	
	public DbManager() {
		conn = DbUtil.getConnection();
	}
	
	public int update(String sql, Object [] params) {
		int count = 0;
		try {
			pstate = conn.prepareStatement(sql);
			for(int i=0;i<params.length;i++) {
				pstate.setObject(i+1, params[i]);
			}
			count = pstate.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}
	
	public ResultSet query(String sql, Object [] params) {
		try {
			pstate = conn.prepareStatement(sql);
			for(int i=0;i<params.length;i++) {
				pstate.setObject(i+1, params[i]);
			}
			rs = pstate.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	
	public void destroy() {
		DbUtil.destroy(conn);
	}
}
